package com.beautiful.xin.sort;

import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xuqingxin 2021/10/9 10:26 上午
 */
public class RepeatNums {
    public static void main(String[] args) {
        int[] nums = {4, 3, 2, 7, 8, 2, 3, 1};
        List<Integer> duplicates = findDuplicates(nums);
        System.out.println(duplicates);
    }

    public static List<Integer> findDuplicates(int[] nums) {
        ArrayList<Integer> result = Lists.newArrayList();

        for (int i = 0; i < nums.length; i++) {
            if (nums[Math.abs(nums[i]) - 1] < 0) {
                result.add(Math.abs(nums[i]));
            } else {
                nums[Math.abs(nums[i]) - 1] *= -1;
            }
        }

        return result;
    }
}
